﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpcUaHelper;
using Opc.Ua.Client;
using Opc.Ua;

namespace Forging2.Services.OPCService
{
    public class OPCServer
    {
        public OpcUaClient OpcUaClient;
        private string[] NodeIDs = Properties.Settings.Default.Nodes.Split(',');
        private string OPCServerPath;
        public OPCServer(string OPCPath)
        {
            OPCServerPath = OPCPath;
            OpcUaClient = new OpcUaClient();
        }
        public bool OPCServerConnected = false;

        public async Task OpenOPCServer()
        {
            try
            {
                //OpcUaClient.UserIdentity = new UserIdentity(new AnonymousIdentityToken());
                await OpcUaClient.ConnectServer(OPCServerPath);
                Console.WriteLine("打开OPC连接： {0}", OPCServerPath);
                OPCServerConnected = OpcUaClient.Connected ? true : false;
            }
            catch (Exception ex)
            {
                Console.WriteLine("OPC连接失败： {0} 错误信息： {1}", OPCServerPath, ex.Message.ToString());
                OPCServerConnected = false;
            }
            
        }
        public void CloseOPCServer()
        {
            OpcUaClient.Disconnect();
            OPCServerConnected = false;
        }

        public async Task<object> ReadPLCValue(int valueType, string NodeId)
        {
            switch (valueType)
            {
                case 0:
                    return await OpcUaClient.ReadNodeAsync<Boolean>(NodeId);

                default:
                    return -1;

            }

        }

        public async Task ReadBooelanPLCValue(string[] nodeids)
        {
            await OpcUaClient.ReadNodesAsync<bool>(nodeids);
        }
        public async Task ReadIntPLCValue(string[] nodeids)
        {
            await OpcUaClient.ReadNodesAsync<int>(nodeids);
        }
        public async Task ReadFloatPLCValue(string[] nodeids)
        {
            await OpcUaClient.ReadNodesAsync<float>(nodeids);
        }
        public async Task ReadShortPLCValue(string[] nodeids)
        {
            await OpcUaClient.ReadNodesAsync<short>(nodeids);
        }


        #region Subscription
        /*
       public void SubscriptionPLCValue(string[] nodeids)
       {
           OpcUaClient.AddSubscription(Environment.MachineName, nodeids, PLCValueSubCallback);
       }

       public void SubscriptionPLCValue()
       {
           OpcUaClient.AddSubscription(Environment.MachineName, NodeIDs, PLCValueSubCallback);
       }
       public void PLCValueSubCallback(string key , MonitoredItem  monitoredItem, MonitoredItemNotificationEventArgs args)
       {
           if (key!=Environment.MachineName)
           {                
               Common.Const.Notification = "机器名不匹配，订阅失败！";
               return;
           }
           GetOPCValue(monitoredItem.StartNodeId.ToString(), args);
       }

       public void GetOPCValue(string monitornode,MonitoredItemNotificationEventArgs args)
       {            
           var equipment = Common.Const.gHeatingPLCAddress.Where(a => a.DB_NODEID == monitornode).FirstOrDefault();
           if (equipment is null)
           {
               Common.Const.Notification = string.Format("没有设备包含此 {0} 节点地址！",monitornode);
               return ;
           }
           var heatingfurnace = Common.Const.gHeatingFurnaces.Where(a => a.GID == equipment.EQUIPMENT_GID).FirstOrDefault();
           MonitoredItemNotification notification = args.NotificationValue as MonitoredItemNotification;
           heatingfurnace.Temperatures = (int)notification.Value.WrappedValue.Value ;
           */
        #endregion





    }
}

